﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using MLM.Data;
using MLM.Base;

namespace MLM.AdminCP.Pages
{
    public partial class CommonSettings : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            Permission.CheckIsAdmin();
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MLMDataContext ct = new MLMDataContext();
                txtEmailHost.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.EMAIL_SMTPHOST);
                txtEmailPort.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.EMAIL_PORT);
                txtEmailUserName.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.EMAIL_USERNAME);
                txtEmailPass.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.EMAIL_PASSWORD);
                txtEmailFrom.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.EMAIL_FROMMAIL);
                txtEmailSendPasswordTemplateEditor.Content = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.EMAIL_SENDNEWPASSWORDTEMPLATE);
                txtEmailRecommendByEmailEditor.Content = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.EMAIL_SENDRECOMMENDTEMPLATE);
                txtWebsiteBaseUrl.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.WEB_BASEURL);
                txtEmailRegisterCompleteEditor.Content = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.REGISTERCOMPLETED);
                txtPrivacyPolicyEditor.Content = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.PRIVACY_POLICY);
                //HEAD INFO
                HeadFaxTxt.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.HEAD_FAX);
                HeadMailTxt.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.HEAD_EMAIL);
                HeadSkypeTxt.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.HEAD_SKYPE);
                HeadPhoneTxt.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.HEAD_PHONE);
                //FEE

                MonthlyBonusPayingFeeTxt.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.FEE_MONTHLY_BONUS_PAYING);
                ShippingFeeTxt.Text = CommonSettingHelper.GetCommonSetting(ct, CommonSettingsContstants.FEE_SHIPPING);
            }
            btnSaveSettings.Click += new EventHandler(btnSaveSettings_Click);
        }

        void btnSaveSettings_Click(object sender, EventArgs e)
        {
            try
            {
                MLMDataContext ct = new MLMDataContext();
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.EMAIL_SMTPHOST, txtEmailHost.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.EMAIL_PORT, txtEmailPort.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.EMAIL_USERNAME, txtEmailUserName.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.EMAIL_PASSWORD, txtEmailPass.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.EMAIL_FROMMAIL, txtEmailFrom.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.EMAIL_SENDNEWPASSWORDTEMPLATE, txtEmailSendPasswordTemplateEditor.Content);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.EMAIL_SENDRECOMMENDTEMPLATE, txtEmailRecommendByEmailEditor.Content);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.WEB_BASEURL, txtWebsiteBaseUrl.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.REGISTERCOMPLETED, txtEmailRegisterCompleteEditor.Content);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.PRIVACY_POLICY, txtPrivacyPolicyEditor.Content);
                //
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.HEAD_EMAIL, HeadMailTxt.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.HEAD_FAX, HeadFaxTxt.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.HEAD_PHONE, HeadPhoneTxt.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.HEAD_SKYPE, HeadSkypeTxt.Text);
                //
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.FEE_MONTHLY_BONUS_PAYING, MonthlyBonusPayingFeeTxt.Text);
                CommonSettingHelper.SaveCommonSetting(ct, CommonSettingsContstants.FEE_SHIPPING, ShippingFeeTxt.Text);
                Message.ShowSuccess();
            }
            catch (Exception ex)
            {
                Message.ShowError(ex.Message);
            }
        }
    }
}